import axios from 'axios';

export default{
    promise_process (promise){
        return promise.then( data => [null, data.data])
                      .catch( err => [err, null]);
    },
    
    async getBooks(){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/books',
            mode            : 'cors',
            method          : 'GET',
            withCredentials: true,
        })
        return response;
    },

    //增加书籍
    async addBook(book){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/books',
            mode            : 'cors',
            method          : 'POST',     
            headers         :{
                "Content-Type": 'application/json'
            },
            withCredentials : true,
            data: book
        })
        return response
    },

    //删除书籍
    async deleteBook(book){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/books',
            mode            : 'cors',
            method          : 'DELETE',     
            headers         :{
                "Content-Type": 'application/json'
            },
            withCredentials : true,
            data: book
        })
        return response
    },
     //修改书籍
     async editBook(book){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/books',
            mode            : 'cors',
            method          : 'PUT',     
            headers         :{
                "Content-Type": 'application/json'
            },
            withCredentials : true,
            data: book
        })
        return response
    },
    //通过ISBN查找该书的借阅历史
    async getLendHistoryByISBN(isbn){
        const response = await axios({
            url             : `http://127.0.0.1:5000/records/bookRecords/${isbn}`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    },

    //通过书名的模糊查找
    async searchByname(name){
        const response = await axios({
            url             : `http://127.0.0.1:5000/books/name/${name}`,
            mode            : 'cors',
            method          : 'GET',
            withCredentials: true,
        })
        return response;
    },

    //通过category的模糊查找
    async searchByCategory(category){
        const response = await axios({
            url             :`/books/category/${category}`,
            mode            : 'cors',
            method          : 'GET',
            withCredentials : true,
        })
        return response
    },
    //通过ISBON的精准查找
    async searchByISBN(isbn){
        const response = await axios({
            url             :`/books/category/${isbn}`,
            mode            : 'cors',
            method          : 'GET',
            withCredentials : true,
        })
        return response
    },

     //借书功能
     async lendBook(amount, book){
        const response = await axios({
            url             : `http://127.0.0.1:5000/lends/${amount}`,
            mode            : 'cors',
            method          : 'POST',     
            headers         :{
                "Content-Type": 'application/json'
            },
            withCredentials : true,
            data: book
        })
        return response
    },

    //还书功能
    async returnBook(data){
        const response = await axios({
            url             : 'http://127.0.0.1:5000/lends',
            mode            : 'cors',
            method          : 'POST',     
            headers         :{
                "Content-Type": 'application/json'
            },
            withCredentials : true,
            data: data
        })
        return response
    },

    //功能：查找某人借阅过的书目
    async checkUserRecord(readerId){
        const response = await axios({
            url             : `http://127.0.0.1:5000/records/book/${readerId}`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    },

    //功能：查找某人借阅历史
    async findLendHistory(readerId){
        const response = await axios({
            url             : `http://127.0.0.1:5000/records/${readerId}`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    },

    //功能： 查询某人当前的借阅书目
    async findReading(readerId){
        const response = await axios({
            url             : `http://127.0.0.1:5000/records/${readerId}/now`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    },

    //查询所有 借阅历史
    async getAllRecords(){
        const response = await axios({
            url             : `http://127.0.0.1:5000/records/${readerId}/now`,
            mode            : 'cors',
            method          : 'GET',     
            withCredentials : true,
        })
        return response
    }
}
